﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Xml.Serialization;

namespace RssReader.Core.Global
{
	public class SerializeEngine<T>
	{
		#region Private Properties

		#endregion

		#region Public Properties

		#endregion

		#region Constructors

		#endregion

		#region Private Methods

		#endregion

		#region Public Methods
		public static void Save(string path, T obj)
		{
			if (!Path.IsPathRooted(path))
			{
				path = HttpContext.Current.Server.MapPath(path);
			}
			if (File.Exists(path))
				File.Delete(path);

			XmlSerializer serial = new XmlSerializer(typeof(T));
			using (Stream stream = File.Open(path, FileMode.Create, FileAccess.ReadWrite))
			{
				serial.Serialize(stream, obj);
			}
		}

		public static T Load(string path)
		{
			if (!Path.IsPathRooted(path))
			{
				path = HttpContext.Current.Server.MapPath(path);
			}
			if (!File.Exists(path))
			{
				throw new FileNotFoundException(path);
			}

			XmlSerializer serial = new XmlSerializer(typeof(T));
			using (Stream stream = File.Open(path, FileMode.Open))
			{
				return (T)serial.Deserialize(stream);
			}
		}
		#endregion
	}
}
